package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.ContactBean;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Tain_v2;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.DrawableTextView;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.GlideRoundTransform;

import java.util.List;


/**
 * 作者：Administrator on 2016/9/28 13:40
 * 邮箱：zhanghuaiha@gmail.com
 */

public class TainFiltrateAdapter extends BaseAdapter implements SectionIndexer {
    private List<Tain_v2> list = null;
    private Context mContext;
    private boolean flag = true;

    public TainFiltrateAdapter(Context mContext, List<Tain_v2> list) {
        this.mContext = mContext;
        this.list = list;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    public void updateListView(List<Tain_v2> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    public View getView(final int position, View view, ViewGroup arg2) {
        ViewHolder viewHolder = null;
        final Tain_v2 tain_v2 = list.get(position);
        Tain_v2.StoreInfoBean store_info = list.get(position).getStore_info();
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_yet_shop, null);
            viewHolder.icon = (ImageView) view.findViewById(R.id.page_info_icon);
            viewHolder.name = (TextView) view.findViewById(R.id.page_name);
            viewHolder.user = (TextView) view.findViewById(R.id.page_user);
            viewHolder.tel = (TextView) view.findViewById(R.id.page_tel);
            viewHolder.location = (TextView) view.findViewById(R.id.page_location);
            viewHolder.yp = (DrawableTextView) view.findViewById(R.id.page_yp);
            viewHolder.lx = (TextView) view.findViewById(R.id.page_lx);
            viewHolder.lxl = (DrawableTextView) view.findViewById(R.id.page_lxl);
            viewHolder.pjzx = (TextView) view.findViewById(R.id.page_pjzx);
            viewHolder.sszx = (TextView) view.findViewById(R.id.page_sszx);
            viewHolder.sslx = (TextView) view.findViewById(R.id.page_sslx);
            viewHolder.mima_yp = (TextView) view.findViewById(R.id.mima_yp);
            viewHolder.mima_sy = (TextView) view.findViewById(R.id.mima_sy);
            viewHolder.wifi_yp = (TextView) view.findViewById(R.id.wifi_yp);
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.wx_contact_catalog);
            viewHolder.wifi_ll = (LinearLayout) view.findViewById(R.id.wifi_ll);
            viewHolder.mima_ll = (LinearLayout) view.findViewById(R.id.mima_ll);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if(position == getPositionForSection(section)){
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(tain_v2.getSortLetters());
        }else{
            viewHolder.tvLetter.setVisibility(View.GONE);
        }

        viewHolder.name.setText(store_info.getName());
        viewHolder.user.setText(store_info.getContact());
        viewHolder.tel.setText(store_info.getTel());

        RequestOptions myOptions = new RequestOptions()
                .transform(new GlideRoundTransform(mContext,10));

        Glide.with(mContext)
                .asBitmap()
                .load(store_info.getImg_url())
                .apply(myOptions)
                .into(viewHolder.icon);

        if (tain_v2.getAlready_num() != null) {
            viewHolder.yp.setText("："+tain_v2.getAlready_num());
        }else {
            viewHolder.yp.setText("：0");
        }



        if (tain_v2.getStatus().equals("1")) {
            viewHolder.lxl.setText("：维护成功");
            viewHolder.lx.setText(mContext.getString(R.string.day_two)+list.get(position).getAlready_num());
        }else {
            viewHolder.lxl.setText("：待维护");
            if (tain_v2.getOk_num() != null) {
                viewHolder.lx.setText(mContext.getString(R.string.hg)+tain_v2.getOk_num());
            }else {
                viewHolder.lx.setText(mContext.getString(R.string.hg)+0);
            }
        }

        if (flag) {
            viewHolder.pjzx.setVisibility(View.VISIBLE);
            viewHolder.sszx.setVisibility(View.VISIBLE);
            viewHolder.sslx.setVisibility(View.VISIBLE);
            if (tain_v2.getOnline_num() != null) {
                viewHolder.sszx.setText(mContext.getString(R.string.sszx)+tain_v2.getOnline_num());
            }else {
                viewHolder.sszx.setText(mContext.getString(R.string.sszx)+0);
            }

            if (tain_v2.getOffline_num() != null) {
                viewHolder.sslx.setText(mContext.getString(R.string.sslx)+tain_v2.getOffline_num());
            }else {
                viewHolder.sslx.setText(mContext.getString(R.string.sslx)+0);
            }

            if (tain_v2.getAvg_online_num() != null) {
                viewHolder.pjzx.setText(mContext.getString(R.string.pjzx)+tain_v2.getAvg_online_num());
            }else {
                viewHolder.pjzx.setText(mContext.getString(R.string.pjzx)+0);
            }
        }else {
            viewHolder.pjzx.setVisibility(View.GONE);
            viewHolder.sszx.setVisibility(View.GONE);
            viewHolder.sslx.setVisibility(View.GONE);
        }


        if (list.get(position).getAlready_num().equals("0")) {
            viewHolder.wifi_ll.setVisibility(View.GONE);
        }else {
            viewHolder.wifi_yp.setText(mContext.getString(R.string.yp)+ list.get(position).getAlready_num());
            viewHolder.wifi_ll.setVisibility(View.VISIBLE);
        }

        if (list.get(position).getMima_already_num().equals("0")) {
            viewHolder.mima_ll.setVisibility(View.GONE);
        }else {
            viewHolder.mima_ll.setVisibility(View.VISIBLE);
            viewHolder.mima_yp.setText(mContext.getString(R.string.yp)+list.get(position).getMima_already_num());
            viewHolder.mima_sy.setText(mContext.getString(R.string.sy)+list.get(position).getMima_ok_num());
        }


        //设置地址
//        holder.location.setText(store_info.getProvince_name()+store_info.getCity_name()+store_info.getArea_name()+store_info.getSpecific_address());
        viewHolder.location.setText(store_info.getArea_name()+store_info.getSpecific_address());
        return view;

    }



    final static class ViewHolder {
        TextView name,user,tel,location,id,lx,pjzx,sszx,sslx,mima_yp,mima_sy,wifi_yp,tvLetter;
        DrawableTextView yp,lxl;
        ImageView icon;
        LinearLayout mima_ll,wifi_ll;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    public void addData(List<Tain_v2> mList){
        if (this.list != null) {
            this.list.clear();
            this.list.addAll(mList);
            notifyDataSetChanged();
        }
    }


    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public Object[] getSections() {
        return null;
    }
}